﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>新闻列表</title>
    <script src="/Apm/js/CMSList.js" type="text/javascript"></script>
    <style>
        
        
    </style>
</head>
<body>
    <div class="page">
        <div class="toolsbar">
            <button id="Add" class="layui-btn">
                <i class="layui-icon">&#xe654;</i> 添加
            </button>
            <button id="Edit" class="layui-btn layui-btn-normal">
                <i class="layui-icon">&#xe642;</i> 编辑
            </button>
            <button id="Delete" class="layui-btn layui-btn-danger">
                <i class="layui-icon">&#xe640;</i> 删除
            </button>
        </div>
        <div class="content">
            <table id="tabledata" lay-filter="datalist"></table>
        </div>
    </div>
</body>
</html>
<script type="text/html" id="IsdisplayTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="Isdisplay" value="{{d.Isdisplay}}" lay-skin="switch" disabled lay-text="是|否" lay-filter="IsdisplayDemo" {{ d.Isdisplay == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="IsweixinTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="Isweixin" value="{{d.Isweixin}}" lay-skin="switch" disabled lay-text="是|否" lay-filter="IsweixinDemo" {{ d.Isweixin == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="TimeTpl">
    {{#
        var FnTime=function(time){
        if(time!=null){
            var date=new Date(time);
            return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
        }
        else{
            return '';
        }
    };
    }}
    <div class="list-time">
        <div class="list-timeadd">
            <span>添加：</span><span>{{FnTime(d.TimeAdd)}}</span>
        </div>
        <div class="list-timeedit">
            <span>修改：</span><span>{{FnTime(d.TimeEdit)}}</span>
        </div>
    </div>
</script>
<script>
    layui.use(['table', 'layer','element'], function () {
        var table = layui.table,
            layer = layui.layer,
            element = layui.element;

        BindData();
        //tab删除后刷新
        parent.OnDeleteTab();
        //新增
        $("#Add").click(function () {
            var guid = new Guid();
            var url = '/Public/New/NewEdit.html?type=add&guid=' + guid.newGuid();
            parent.AddTab('PublicNewAdd', url, '<img src="/icon/computer_go.png" />', '添加新闻');
        })
        //编辑
        $("#Edit").click(function () {
            var checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值

            //console.log(checkStatus.data) //获取选中行的数据
            //console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
            //console.log(checkStatus.isAll) //表格是否全选

            var rows= checkStatus.data.length;
            if (rows > 0) {
                if (rows == 1) {
                    var guid = checkStatus.data[0].Guid;
                    var url = '/Public/New/NewEdit.html?type=edit&guid=' + guid;
                    parent.AddTab('PublicNewEdit', url, '<img src="/icon/computer_go.png" />', '编辑新闻');
                }
                else {
                    layer.msg("编辑中请不要多选");
                }
            }
            else {
                layer.msg("请选中一行");
            }
            
        })
        //删除
        $("#Delete").click(function () {
            var checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值

            var rows = checkStatus.data.length;
            if (rows > 0) {
                var guidlist = [];
                for (var i = 0; i < rows; i++) {
                    guidlist.push(checkStatus.data[i].Guid);
                }
                layer.confirm('确定删除？', function (index) {
                    layer.close(index);
                    var json = new Object();
                    json.GuidList = guidlist;

                    //加载
                    var loadindex = layer.load(1); 
                    $.ajax({
                        url: ApiURL + 'api/Method/DeleteNew?token=' + MyPublic.getToken(),
                        type: 'post',
                        contentType: 'application/json',
                        data: JSON.stringify(json),
                        success: function (data) {
                            BindData();
                            layer.close(loadindex);
                        },
                        error: function () {
                            location.href = "/error.html";
                        }
                    })
                });
            }
            else {
                layer.msg("请选择你要删除的项");
            }
        })
    });
    //数据绑定
    function BindData() {
        layui.use(['table', 'layer'], function () {
            var table = layui.table,
                layer = layui.layer;

            //获取列表数据
            $.ajax({
                url: ApiURL + 'api/Method/GetNewList?token=' + MyPublic.getToken(),
                type: 'get',
                contentType: 'application/json',
                success: function (data) {
                    table.render({
                        id: 'datalist',
                        elem: '#tabledata',
                        cols: [[//标题栏
                            { type: 'checkbox' },
                            { field: 'Title', title: '标题', },
                            { field: 'Introduce', title: '内容' },
                            { field: 'Time', title: '时间', templet: '#TimeTpl',width:176 },
                            { field: 'Isdisplay', title: '是否显示', templet: '#IsdisplayTpl' },
                            { field: 'Isweixin', title: '是否微信显示', templet: '#IsweixinTpl' },
                            { field: 'PersonalName', title: '添加人' }
                        ]],
                        data: data,
                        height: 'full-68',
                        limits: [10, 30, 60],
                        page: true
                    })
                },
                error: function () {
                    location.href = "/error.html";

                }
            })
        });
    }
</script>